__author__ = 'drazen.popovic'

from socket import socket
from StringIO import StringIO
from Parser import *

if __name__ == '__main__':
    s = socket()
    s.connect(("127.0.0.1",1337))
    fileSocket = s.makefile(mode="r+")
    #step 1: send price calc request
    try:
        stringCalcPriceRequest = "150076\t01\t98\t\t8825#KT517J#1\t762159\t00\t201005171913\t\t\t\t\t\t\t\t20100517191302915\r\n"
        streamCalcPriceReq = StringIO(stringCalcPriceRequest)
        pduCalcPriceReq = PDUCalculatePriceReq()
        pduCalcPriceReq.readFromStream(streamCalcPriceReq)
        pduCalcPriceReq.writeToStream(fileSocket)
    except Exception:
        print "Error sending CalcPriceReq"
    #step 2: recv price calc response
    try:
        pduCalcPriceResp = PDUCalculatePriceResp()
        pduCalcPriceResp.readFromStream(fileSocket)
    except Exception:
        print "Error receiving CalcPriceResp"
    #step 3: send transaction status
    try:
        stringTransactionStatus = "150064\t03\t98\t\t8825#KT517J#1\t762159\t00\t\t\t\t\t\t\t\t\t20100517191318087\r\n"
        streamTransactionStatus = StringToStream(stringTransactionStatus)
        pduTransactionStatus = PDUTransactionStatus()
        pduTransactionStatus.readFromStream(streamTransactionStatus)
        pduTransactionStatus.write(fileSocket)
    except Exception:
        print "Error sending TransactionStatus"
    #step 4: recv transaction success
    try:
        pduTransactionSuccess = PDUTransactionSuccess()
        pduTransactionSuccess.readFromStream(fileSocket)
        print str(pduTransactionSuccess)
    except Exception:
        print "Error receiving TransactionSuccess"

    s.close()
